#!/bin/sh

# Author: Zhang Huangbin (michaelbibby <at> gmail.com)
# $Date$
# $Revision$

. ./conf/global
. ./conf/functions
. ./conf/openldap

trap "exit 255" 2

check_arch

# -------------------------------------------------------------
# Use 'dialog' for configuration.
# -------------------------------------------------------------
# Install 'dialog' first.
which dialog >/dev/null 2>&1
[ X"$?" == X"0" ] || install_pkg dialog
if [ X"$?" != X"0" ]; then
    echo "${OUTPUT_FLAG} Install 'dialog' failed. Please install it first and make"
    echo "${OUTPUT_FLAG} sure your yum client works well."
    exit 255
else
    :
fi

DIALOG='dialog --no-collapse'
DIALOG_BACKTITLE='Mail Server Installation Wizard for RHEL/CentOS 5.x'

# Welcome message.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Welcome and thanks for use" \
    --msgbox "\
Thanks for your use of this script for mail server.
Feedback, bug report, communication are all welcome.

Contact me if you need help about this script or RHEL:

    * Author:       Bibby
    * Mail/GTalk:   michaelbibby (at) gmail.com
    * MSN:          michaelbibby (at) hotmail.com

NOTE:

    Ctrl-C will abort this wizard.
" 20 76

# Main features.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Main Features" \
    --msgbox "\
The mail server contains the following main features:

* SMTP (Use Postfix, with LDAP & MySQL support)
* POP3/IMAP (Use Courier POP3/IMAP)
* Virtual Domains/Users (Use OpenLDAP or MySQL)
* Anti-Spam (Use SpamAssassin)
* Anti-Virus (Use ClamAV)
* Web Mail
    * SquirrelMail for LDAP backend. i18n support, with useful plugins
    * Roundcube for MySQL backend. Ajax web mail
" 20 76

# VMAIL_USER_HOME_DIR
VMAIL_USER_HOME_DIR="/home/vmail"
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "HOME directory of VMAIL user" \
    --inputbox "\
Please specify the HOME directory of vmail user: ${VMAIL_USER_NAME}.

EXAMPLE:

    * ${VMAIL_USER_HOME_DIR}

NOTE:

    * All mails will be stored in this HOME directory, so it may take
      large disk space.
" 20 76 "${VMAIL_USER_HOME_DIR}" 2>/tmp/vmail_user_home_dir

VMAIL_USER_HOME_DIR="$(cat /tmp/vmail_user_home_dir)"
echo "VMAIL_USER_HOME_DIR='${VMAIL_USER_HOME_DIR}'" >${CONFIG_FILE}
rm -f /tmp/vmail_user_home_dir

# --------------------------------------------------
# --------------------- Backend --------------------
# --------------------------------------------------
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Choose your prefer backend" \
    --radiolist "\
We provide two backends and the homologous webmail programs:

    +----------+--------------+--------------+
    | Backend  | Web Mail     | Admin tool   |
    +----------+--------------+--------------+
    | OpenLDAP | SquirrelMail | phpLDAPadmin |
    +----------+--------------+--------------+
    | MySQL    | Roundcube    | PostfixAdmin |
    +----------+--------------+--------------+

TIP:
    Use 'Space' key to select item.

" 20 76 2 \
    "OpenLDAP" "An open source implementation of LDAP protocol. " "on" \
    "MySQL" "The world's most popular open source database." "off" \
    2>/tmp/backend

BACKEND="$(cat /tmp/backend)"
echo "BACKEND='${BACKEND}'" >> ${CONFIG_FILE}

if [ X"${BACKEND}" == X"OpenLDAP" ]; then
    . ${TOOLS_DIR}/ldap_config.sh
else
    . ${TOOLS_DIR}/mysql_config.sh
fi

#
# Mailing List Support.
#
#${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
#    --title "Advanced Mailing List Support" \
#    --radiolist "\
#Do you need any advanced mailing list manager for your mail system?
#
#" 20 76 2 \
#    "No" "Do *NOT* use any mailing list manager." "off" \
#    "Mailman" "Use GNU Mailman as Mailing List Manager." "on" \
#    2>/tmp/maillist
#
#MAILLIST="$(cat /tmp/maillist)"
#rm -f /tmp/maillist
#echo "MAILLIST='${MAILLIST}'" >> ${CONFIG_FILE}

#if [ X"${MAILLIST}" == X"Mailman" ]; then
#    . ${TOOLS_DIR}/mailman_config.sh
#else
#    :
#fi

#
# Set mail alias for root.
#
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Specify mail alias for 'root' user" \
    --inputbox "\
Please specify a *E-Mail* address for 'root' user alias.

Mail deliver failure notice will send to this alias address instead of
system account 'root'.

EXAMPLE:

    * michaelbibby@gmail.com
" 20 76 2>/tmp/mail_alias_root

MAIL_ALIAS_ROOT=$(cat /tmp/mail_alias_root)
rm -f /tmp/mail_alias_root
echo "MAIL_ALIAS_ROOT=${MAIL_ALIAS_ROOT}" >> ${CONFIG_FILE}

#
# Ending message.
#
cat <<EOF
Configuration completed.

Warnning:

    Please do remember to remove configuration file after installation
    completed successfully.

        * ${CONFIG_FILE}
EOF

echo -en "\n${OUTPUT_FLAG} Continue? [y|N]"
read ANSWER

case ${ANSWER} in
    Y|y )
        :
        ;;
    N|n|* )
        echo "${OUTPUT_FLAG} Canceled, Exit."
        exit 255
        ;;
esac
